<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class User_Model extends CI_Model {
	
	private $table= 'users';
	
	function __construct() {
		parent::__construct();
		$this->load->library('session');
	}
	
	function login ($username, $password){
		$password = CommonUtils::encryptPassword($password);
    	$query = $this->db->get_where($this->table, array('username' => $username,'password'=>$password, 'is_active'=>1));
    	#$result = $query->result();		
		if($query->num_rows() > 0){
			$this->session->set_userdata(
				array(
					'user_id' => $query->row()->id,
					'username' => $query->row()->username,
					'client_id' => $query->row()->client_id,
					'isLogged' => true,
					'isSuperAdmin' => $query->row()->superadmin,
				)
			);
			//$this->session->set_userdata('isLogged', true);
			return true;
		}
		return false;
		
    	//return !empty($result);
	}
	
	function saveSession($data){
		$this->session->set_userdata(
			array(
				'user_id' => $data->user_id,
				'username' => $data->username,
				'client_id' => $data->client_id,
				'isLogged' => true,
				'role'=>$data->role
			)
		);
	}
	
	function save($data){		
		$this->db->insert($this->table, $data);
		return $this->db->insert_id();
	}
	
	function getAllUsers(){
		$query = $this->db->get_where($this->table, array('client_id' => $this->session->userdata('client_id')));
		$result = $query->result();
		return $result;
	}
	
	function getUserProfile($user_id){
		$query = $this->db->get_where('file201', array('user_id' => $user_id));
		$result = $query->result();
		return $result;
	}
	
	function saveUserProfile($data){
		$query = $this->db->get_where('file201', array('user_id' => $data['user_id']));
		$result = $query->result();
		if(empty($result)){
			$this->db->insert('file201', $data);
			return $this->db->insert_id();
		}else{
			$this->db->where('user_id', $data['user_id']);
			$this->db->update('file201', $data); 
			return true;
		}
	}
	
	function getEducation($user_id){
		$query = $this->db->get_where('education', array('user_id' => $user_id));
		$result = $query->result();
		return $result;
	}
	
	function saveEducation($data){

		$dbdata1['user_id'] = $data['user_id'];
		$dbdata2['user_id'] = $data['user_id'];
		$dbdata3['user_id'] = $data['user_id'];	
		
		foreach($data['education'] as $key=>$value){
				$dbdata1[$key] = $value[0];
				$dbdata2[$key] = $value[1];
				$dbdata3[$key] = $value[2];
		}
	
		$query = $this->db->get_where('education', array('user_id' => $data['user_id']));
		$result = $query->result();
		
		if(empty($result)){			
			
			$this->db->insert('education', $dbdata1);
			$this->db->insert('education', $dbdata2);
			$this->db->insert('education', $dbdata3);
			
		}else{
		
		    $dbdata = array($dbdata1, $dbdata2, $dbdata3);
			
			foreach($dbdata as $value){
				$this->db->where('id', $value['id']);
				$this->db->update('education', $value); 
			}
			
			#return true;
		}
	}
	
	function getEmployment($user_id){
		$query = $this->db->get_where('employment', array('user_id' => $user_id));
		$result = $query->result();
		return $result;
	}
	
	function saveEmployment($data){

		$dbdata1['user_id'] = $data['user_id'];
		$dbdata2['user_id'] = $data['user_id'];
		$dbdata3['user_id'] = $data['user_id'];	
		
		foreach($data['employment'] as $key=>$value){
				$dbdata1[$key] = $value[0];
				$dbdata2[$key] = $value[1];
				$dbdata3[$key] = $value[2];
		}
	
		$query = $this->db->get_where('employment', array('user_id' => $data['user_id']));
		$result = $query->result();
		
		if(empty($result)){			
			
			$this->db->insert('employment', $dbdata1);
			$this->db->insert('employment', $dbdata2);
			$this->db->insert('employment', $dbdata3);
			
		}else{
		
		    $dbdata = array($dbdata1, $dbdata2, $dbdata3);
			
			foreach($dbdata as $value){
				$this->db->where('id', $value['id']);
				$this->db->update('employment', $value); 
			}
			
			#return true;
		}
	}
	
	function getDependents($user_id){
		$query = $this->db->get_where('dependents', array('user_id' => $user_id));
		$result = $query->result();
		return $result;
	}
	
	function saveDependents($data){
		
		foreach($data['dependents'] as $key=>$value){
				$dbdata1[$key] = $value[0];
				$dbdata2[$key] = $value[1];
				$dbdata3[$key] = $value[2];
		}

		$query = $this->db->get_where('dependents', array('user_id' => $data['user_id']));
		$result = $query->result();
		
		if(empty($result)){			
			
			$this->db->insert('dependents', $dbdata1);			
			#$this->db->insert('user_dependents', array('user_id' => $data['user_id'], 'dependent_id' => $this->db->insert_id()));
			$this->db->insert('dependents', $dbdata2);
			#$this->db->insert('user_dependents', array('user_id' => $data['user_id'], 'dependent_id' => $this->db->insert_id()));
			$this->db->insert('dependents', $dbdata3);
			#$this->db->insert('user_dependents', array('user_id' => $data['user_id'], 'dependent_id' => $this->db->insert_id()));
			
		}else{
		
		    $dbdata = array($dbdata1, $dbdata2, $dbdata3);
			
			foreach($dbdata as $value){
				$this->db->where('id', $value['id']);
				$this->db->update('dependents', $value); 
			}
			
			#return true;
		}
	}
	
	function isClientAllowed($clientId, $userId) {
		$query = $this->db->get_where($this->table, array('id' => $userId,'client_id'=>$clientId));
    	$result = $query->result();
    	return !empty($result);
	}
	

}